using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
// using Text = UnityEngine.UI.Text;
// using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;


namespace EmailPanel
{
    public class EmailBtn : MonoBehaviour
    {
        private DetailCtrl detail_ctrl;
        private Transform email_parent_trans;

        public int obj_id = -1;

        public void btn_on_click()
        {
            if (obj_id == detail_ctrl.current_email_index)
                return;
            
            BdPackage.BdUtils.log($"--- btn_on_click! obj_id: {obj_id}");

            int email_num = email_parent_trans.childCount;
            for (int i = 0; i < email_num; i ++)
            {
                var email_i = email_parent_trans.GetChild(i);
                var image_i = email_i.transform.Find("Icon/Image").GetComponent<Image>();
                var sender_img = email_i.transform.Find("Sender/symbol").GetComponent<Image>();

                // int child_id = BdPackage.BdUtils.get_obj_index_by_name(email_i.name);
                // BdPackage.BdUtils.log("--- id:", email_i.name, "--->", child_id, "--- current:", detail_ctrl.current_email_index, "--- btn obj_id:", obj_id);

                if (email_i == transform)
                {
                    image_i.color = Color.white;
                    
                    sender_img.color = Color.gray;
                    email_i.Find("Title").GetComponent<Text>().color = Color.gray;
                    email_i.Find("Sender/number_text").GetComponent<Text>().color = Color.gray;
                    email_i.Find("ExpirationTime").GetComponent<Text>().color = Color.gray;
                    continue;
                }
                else
                {
                    var new_color = BdPackage.BdTools.ColorTools.change_color_alpha(Color.white, 0.5f);
                    // BdPackage.BdUtils.log("--- new_color:", new_color);
                    image_i.color = new_color;
                    sender_img.color = new_color;
                    
                    email_i.Find("Title").GetComponent<Text>().color = new_color;
                    email_i.Find("Sender/number_text").GetComponent<Text>().color = new_color;
                    email_i.Find("ExpirationTime").GetComponent<Text>().color = new_color;
                }
                
                var selected_i = email_i.Find("Selected");
                selected_i.gameObject.SetActive(false);
            }
            var email_info_i = detail_ctrl.email_info_dc[obj_id];
            
            detail_ctrl.detail_title.text = email_info_i.biao_ti;
            detail_ctrl.detail_sender.text = email_info_i.fa_jian_ren;
            detail_ctrl.detail_time.text = email_info_i.fa_song_shi_jian.Split(" ")[0];
            detail_ctrl.detail_content.text = email_info_i.nei_rong;
            
            // BdPackage.BdUtils.log("~~~ email_info_i.fu_jian_dc_ls --- ", email_info_i.fu_jian_dc_ls);
            MyUnityUtils.BaseUtils.clear_children(detail_ctrl.detail_attachment_parent_obj);
            
            foreach (var item_dc_i in email_info_i.fu_jian_dc_ls)
            {
                int item_id = item_dc_i["id"];
                var row_i = AppTableUtils.TableUtils.get(detail_ctrl.item_info_dt, item_id);
                string item_prefab_name = BdPackage.BdUtils.conv_to_string(row_i["预制体"]);

                string tem_prefab_dir_path = "Prefabs/icons";
                GameObject item_obj = ObjectPool.instance.GetObj(item_prefab_name, tem_prefab_dir_path);
                item_obj.transform.SetParent(detail_ctrl.detail_attachment_parent_obj.transform);

                int item_num = item_dc_i["num"];
                item_obj.transform.Find("Number/Text").GetComponent<Text>().text = $"{item_num}";
                
                item_obj.transform.localScale = Vector3.one;
            }
            
            detail_ctrl.detail_btn_parent_obj.SetActive(email_info_i.fu_jian_dc_ls.Count > 0);

            detail_ctrl.update_file_obj(obj_id);
        }

        private void Awake()
        {
            detail_ctrl = transform.GetComponentInParent<DetailCtrl>();
            email_parent_trans = detail_ctrl.scroll_content_parent_obj.transform;
        }
    }
}